package com.huatai.datadevelop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.api.R;
import com.huatai.datadevelop.entity.ApiSubscreptionEntity;
import com.huatai.datadevelop.service.RealTimeService;
import com.huatai.datadevelop.vo.DeleteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO
 * @date 2023/2/13 15:16
 * @Version 1.0
 */
@RestController
@Api(value = "实时数据推送模块",tags = "实时数据推送模块")
@RequestMapping("/realTime")
public class RealTimeController {

    @Autowired
    private RealTimeService realTimeService;

    /**
     * 根据订阅接口推送数据
     * @param interfaceName
     * @return
     */
	@ApiOperation("根据订阅接口推送数据")
    @PostMapping(value = "/pushData/{interfaceName}")
    public R pushData(@RequestBody JSONObject data,
					  @PathVariable(value = "interfaceName",required = true) String interfaceName) throws IOException {
        realTimeService.pushData(data,interfaceName);
        return R.success("成功");
    }
    /**
     * 获取列表
     */
	@ApiOperation("获取列表")
    @PostMapping("/api/list")
    public R list(@RequestBody Map<String, Object> params){
        IPage page = realTimeService.getPage(params);
        return R.data(page);
    }

    @PostMapping("/api/getApi")
	@ApiOperation("getApi")
    public R getApi(@RequestParam Integer apiSubId){
        return realTimeService.getApi(apiSubId);
    }

    /**
     * 新增或更新
     * docId不存在或小于等于0时   即为新增
     * @return
     */
	@ApiOperation("新增或更新")
    @PostMapping("/api/save")
    public R addApi(@RequestBody ApiSubscreptionEntity apiSubscreption){
        String subscription = apiSubscreption.getSubscription();
        if (subscription==null || !subscription.matches("[a-zA-Z]+")){
            return R.fail("接口subscription必须全英文");
        }
        return realTimeService.saveOrUpdateApi(apiSubscreption);
    }

	@ApiOperation("添加前置判断 subscription是否存在")
    @PostMapping("/api/getSubscription")
    public R getSubscription(String subscription){
        return realTimeService.getSubscription(subscription);
    }

    /**
     * 批量删除
     * @return
     */
	@ApiOperation("批量删除")
    @PostMapping("/api/deleteBatch")
    public R deleteApiBatch(@RequestBody DeleteVO data){
        return realTimeService.deleteApiSub(data.getIds());
    }
}
